package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.bean.HouseInfoBean;
import com.wnhz.work.yz.bean.HouseMoneyBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---住宿要求
 */
public class StayHouseActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stay_house);
        getHolseInfo();
        initData();
    }

    private List<HouseInfoBean.InfoEntity> info1 = new ArrayList<>();

    private void getHolseInfo() {
        Map<String, Object> map = new HashMap<>();
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.INFORMATION_HOTELSTANDARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        HouseInfoBean bean = gson.fromJson(result, HouseInfoBean.class);
                        info1 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                getHouse2Info();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private List<HouseMoneyBean.InfoEntity> info2 = new ArrayList<>();

    private void getHouse2Info() {
        Map<String, Object> map = new HashMap<>();
        XUtil.Post(Url.INFORMATION_HOTELPRICESTANDARD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        HouseMoneyBean bean = gson.fromJson(result, HouseMoneyBean.class);
                        info2 = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private ApplyMessagebean.VisitInfoEntity.HotelInfoEntity hotel_info = new ApplyMessagebean.VisitInfoEntity.HotelInfoEntity();

    private void initData() {
        hotel_info = ApplyMessageActivity.bean.getVisit_info().getHotel_info();
    }

    private TextView tv_house, tv_price;
    private EditText et_standard_room, et_ssingle_room;

    private void initView() {
        tv_house = (TextView) findViewById(R.id.tv_house);
        tv_price = (TextView) findViewById(R.id.tv_price);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("住宿要求");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setText("保存");
        tv_right.setTextColor(Color.parseColor("#008c99"));
        tv_right.setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);

        findViewById(R.id.rl_stay_standard).setOnClickListener(this);
        findViewById(R.id.rl_price).setOnClickListener(this);
        et_standard_room = (EditText) findViewById(R.id.et_standard_room);
        et_ssingle_room = (EditText) findViewById(R.id.et_ssingle_room);
        if (!hotel_info.getHotel_level().toString().trim().equals("")) {
            tv_house.setText(hotel_info.getHotel_level());
            tv_price.setText(hotel_info.getHotel_price());
            et_ssingle_room.setText(hotel_info.getSingel_num());
            et_standard_room.setText(hotel_info.getDouble_num());
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //保存
            case R.id.submit:
//                MyToast("保存");
                if (tv_house.getText().toString().trim().equals("")) {
                    MyToast("请选择住宿标准");
                    return;
                }
                if (tv_price.getText().toString().trim().equals("")) {
                    MyToast("请选择价格");
                    return;
                }
                if (et_standard_room.getText().toString().trim().equals("")) {
                    MyToast("请填写标间数");
                    return;
                }
                if (et_ssingle_room.getText().toString().trim().equals("")) {
                    MyToast("请填写单间数");
                    return;
                }
                hotel_info.setDouble_num(et_standard_room.getText().toString().trim());
                hotel_info.setSingel_num(et_ssingle_room.getText().toString().trim());
                hotel_info.setHotel_price(tv_price.getText().toString().trim());
                hotel_info.setHotel_level(tv_house.getText().toString().trim());
                hotel_info.setIs_hotel("1");
                setResult(RESULT_OK);
                finish();
                break;

            //住宿标准
            case R.id.rl_stay_standard: {
                // MyToast("住宿标准");
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info1.size(); i++) {
                    str.add(info1.get(i).getHotel_level());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_house.setText(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();

            }
            break;

            //价格区间
            case R.id.rl_price:
                // MyToast("价格区间");
            {
                final List<String> str = new ArrayList<>();
                for (int i = 0; i < info2.size(); i++) {
                    str.add(info2.get(i).getHotel_price());
                }

                OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        tv_price.setText(str.get(options1));
                    }
                })
                        //.setTitleSize(16)//标题文字大小
                        // .setTitleText("预算系数")
                        .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
                        .setTitleColor(getResources().getColor(R.color.text))//标题文字颜色)//标题文字颜色
                        .setSubmitColor(getResources().getColor(R.color.main_color))//确定按钮文字颜色
                        .setCancelColor(getResources().getColor(R.color.main_color))//取消按钮文字颜色
                        .build();
                pvOptions.setPicker(str);
                pvOptions.show();
            }

            break;

            default:
                break;
        }
    }
}
